package rgbos.forum.persistance;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;
import org.w3c.dom.DOMException;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import rgbos.forum.domain.Auditor;
import rgbos.forum.domain.Post;

public class XMLPost extends XMLUtil{
	private String postFileName_ = "posts.xml";
	private DateFormat dateFormat_;

	public XMLPost() {
		dateFormat_ =  new SimpleDateFormat("dd/MM/yyyy");
	}
	//post actions
	public int saveNewPost(Post post,int topicId) {
		// need to update in the topicId
		Element root = getPostDoc_().getDocumentElement();
		try {
			NodeList tempNl = getPostDoc_().getElementsByTagName("Post");
			for (int i = 0; i < tempNl.getLength(); i++) {
				Node tempNode = tempNl.item(i);
				NamedNodeMap tempNMap = tempNode.getAttributes();
				int id = Integer.valueOf(tempNMap.getNamedItem("ID").getNodeValue());
				if (id == post.getId_()){
					Auditor.Log("The post " + post.getId_() + " was ALLREADY added to the forum.", this.getClass());
					return -1;
				}
			}
			Element nodePost = getPostDoc_().createElement("Post");
			nodePost.setAttribute("ID", String.valueOf(post.getId_()));
			nodePost.setAttribute("title", post.getTitle_());
			nodePost.setAttribute("authorName", post.getAuthorName());
			nodePost.setAttribute("creationDate", dateFormat_.format(post.getCreationDate_()));
			nodePost.setAttribute("body", post.getBody_());
			root.appendChild(nodePost);

		} catch (Exception e) {
			return -1;
		}
		Auditor.Log("The post " + post.getId_() + " was added to the forum.", this.getClass());
		//modify counter :
		int tempCounter = Integer.valueOf(root.getAttribute("Counter"));
		root.setAttribute("Counter",String.valueOf(tempCounter+1));
		// writing the new XML to file
		XMLUtil.writeXMLFile(getPostDoc_(), postFileName_);
		return post.getId_();
	}
	public boolean modifyPost(Post post) {
		try {
			NodeList tempNl = getPostDoc_().getElementsByTagName("Post");
			for (int i = 0; i < tempNl.getLength(); i++) {
				Node tempNode = tempNl.item(i);
				NamedNodeMap tempNMap = tempNode.getAttributes();
				int id = Integer.valueOf(tempNMap.getNamedItem("ID").getNodeValue());
				if (id == post.getId_()) {

					Element newChild = getPostDoc_().createElement("Post");
					newChild.setAttribute("ID", String.valueOf(post.getId_()));
					newChild.setAttribute("title", post.getTitle_());
					newChild.setAttribute("authorName", post.getAuthorName());
					newChild.setAttribute("creationDate", dateFormat_.format(post.getCreationDate_()));
					newChild.setAttribute("body", post.getBody_());
					tempNode.getParentNode().replaceChild(newChild, tempNode);
					Auditor.Log("The post " + post.getId_() + " was modified", this.getClass());
					XMLUtil.writeXMLFile(getPostDoc_(), postFileName_);
					return true;
				}
			}
		} catch (DOMException e) {
			return false;
		}
		return false;
	}
	public boolean modifyPostBody(int postId,String body) {
		try {
			NodeList tempNl = getPostDoc_().getElementsByTagName("Post");
			for (int i = 0; i < tempNl.getLength(); i++) {
				Node tempNode = tempNl.item(i);
				NamedNodeMap tempNMap = tempNode.getAttributes();
				int id = Integer.valueOf(tempNMap.getNamedItem("ID").getNodeValue());
				if (id == postId) {

					tempNMap.getNamedItem("body").setNodeValue(body);

					Auditor.Log("The post " + postId + " body was modified", this.getClass());
					XMLUtil.writeXMLFile(getPostDoc_(), postFileName_);
					return true;
				}
			}
		} catch (DOMException e) {
			return false;
		}
		return false;
	}
	public boolean deletePost(int postId) {
		Element root = getPostDoc_().getDocumentElement();
		try {
			NodeList tempNl = getPostDoc_().getElementsByTagName("Post");
			for (int i = 0; i < tempNl.getLength(); i++) {
				Node tempNode = tempNl.item(i);
				NamedNodeMap tempNMap = tempNode.getAttributes();
				int id = Integer.valueOf(tempNMap.getNamedItem("ID").getNodeValue());
				if (id == postId) {

					tempNode.getParentNode().removeChild(tempNode);
					//modify counter :
					int tempCounter = Integer.valueOf(root.getAttribute("Counter"));
					root.setAttribute("Counter",String.valueOf(tempCounter-1));
					XMLUtil.writeXMLFile(getPostDoc_(), postFileName_);
					Auditor.Log("The post " + postId + " was deleted from the forum ", this.getClass());
					return true;
				}
			}
		} catch (DOMException e) {
			return false;
		}
		Auditor.Log("The post " + postId + " was All ready deleted from the forum.", this.getClass());
		return false;
	}
	public Post getPostById(int postId){
		Post post = null;
		NodeList tempNl = getPostDoc_().getElementsByTagName("Post");
		for (int i = 0; i < tempNl.getLength(); i++) {
			Node tempNode = tempNl.item(i);
			NamedNodeMap tempNMap = tempNode.getAttributes();
			int id = Integer.valueOf(tempNMap.getNamedItem("ID").getNodeValue());
			if (id == postId){

				String title = tempNMap.getNamedItem("title").getNodeValue();
				String authorName = tempNMap.getNamedItem("authorName").getNodeValue();
				Date creationDate = null;
				try {
					creationDate = dateFormat_.parse(tempNMap.getNamedItem("creationDate").getNodeValue());
				} catch (DOMException e) {
					e.printStackTrace();
				} catch (ParseException e) {
					e.printStackTrace();
				}
				String body = tempNMap.getNamedItem("body").getNodeValue();
				post = new Post(id, title, authorName, creationDate, body);
				return post;
			}
		}
		Auditor.Log("The post " + postId + " not in the DataBase.", this.getClass());
		return null;
	}
	//Getters:
	public Vector<Post> getPosts() {
		Vector<Post> posts = new Vector<Post>();
		NodeList tempNl = getPostDoc_().getElementsByTagName("Post");
		for (int i = 0; i < tempNl.getLength(); i++) {
			Node tempNode = tempNl.item(i);
			NamedNodeMap tempNMap = tempNode.getAttributes();
			int id = Integer.valueOf(tempNMap.getNamedItem("ID").getNodeValue());
			String title = tempNMap.getNamedItem("title").getNodeValue();
			String authorName = tempNMap.getNamedItem("authorName").getNodeValue();
			Date creationDate = null;
			try {
				creationDate = dateFormat_.parse(tempNMap.getNamedItem("creationDate").getNodeValue());
			} catch (DOMException e) {
				e.printStackTrace();
			} catch (ParseException e) {
				e.printStackTrace();
			}
			String body = tempNMap.getNamedItem("body").getNodeValue();
			Post tmpPost = new Post(id, title, authorName, creationDate, body);
			posts.add(tmpPost);
		}
		return posts;
	}
}
